<html>
<head><meta charset="utf-8"><title>Making *const fn in const fn · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html">Making *const fn in const fn</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201558533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201558533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Avi Dessauer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201558533">(Jun 21 2020 at 23:55)</a>:</h4>
<p>This particularly <code>mem::transmute(&amp;T::evacuate)</code> and friends feels horribly wrong.<br>
I imagine it's undefined?</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">GcTypeInfo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="n">evacuate_fn</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="k">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">Range</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">Handlers</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="n">transitive_gc_types_fn</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="k">fn</span><span class="p">(</span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">Tti</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="sd">/// `direct_gc_types(&amp;mut HashMap&lt;GcTypeInfo, ([offset: u8], bits: u8)&gt;, starting_offset: u8 = 0)`</span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="n">direct_gc_types_fn</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="k">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">HashMap</span><span class="o">&lt;</span><span class="n">GcTypeInfo</span><span class="p">,</span><span class="w"> </span><span class="n">TypeRow</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="n">needs_drop</span>: <span class="kt">bool</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="n">size</span>: <span class="kt">u16</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="p">(</span><span class="k">crate</span><span class="p">)</span><span class="w"> </span><span class="n">align</span>: <span class="kt">u16</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">GcTypeInfo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">Condemned</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="bp">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">evacuate_fn</span>: <span class="nc">mem</span>::<span class="n">transmute</span><span class="p">(</span><span class="o">&amp;</span><span class="n">T</span>::<span class="n">evacuate</span><span class="p">),</span><span class="w"></span>
<span class="w">                </span><span class="n">transitive_gc_types_fn</span>: <span class="nc">mem</span>::<span class="n">transmute</span><span class="p">(</span><span class="o">&amp;</span><span class="n">T</span>::<span class="n">transitive_gc_types</span><span class="p">),</span><span class="w"></span>
<span class="w">                </span><span class="n">direct_gc_types_fn</span>: <span class="nc">mem</span>::<span class="n">transmute</span><span class="p">(</span><span class="o">&amp;</span><span class="n">T</span>::<span class="n">direct_gc_types</span><span class="p">),</span><span class="w"></span>
<span class="w">                </span><span class="n">needs_drop</span>: <span class="nc">mem</span>::<span class="n">needs_drop</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">size</span>: <span class="nc">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u16</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="n">align</span>: <span class="nc">mem</span>::<span class="n">align_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u16</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="201574391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201574391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201574391">(Jun 22 2020 at 07:25)</a>:</h4>
<p>I don't think so -- not until you actually try calling a non-const fn during CTFE, at which point you will get an error.</p>



<a name="201574407"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201574407" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201574407">(Jun 22 2020 at 07:25)</a>:</h4>
<p>oh wait</p>



<a name="201574411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201574411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201574411">(Jun 22 2020 at 07:25)</a>:</h4>
<p>I think I misread</p>



<a name="201574419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201574419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201574419">(Jun 22 2020 at 07:25)</a>:</h4>
<p>this is a  const raw pointer to a fn pointer</p>



<a name="201574448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201574448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201574448">(Jun 22 2020 at 07:25)</a>:</h4>
<p>so there is a double indirection.<br>
I mean, creating such a thing is not undefined, but what do you want to <em>do</em> with it?</p>



<a name="201574513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201574513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201574513">(Jun 22 2020 at 07:26)</a>:</h4>
<p>and why dont you just <code>&amp;T::evacuate as *const _</code>?</p>



<a name="201611814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201611814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Avi Dessauer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201611814">(Jun 22 2020 at 14:25)</a>:</h4>
<p>The function pointers are only called at run time. I use transmute as a convenience to turn <code>&amp;T</code> arguments into <code>*const u8</code></p>



<a name="201619937"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201619937" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201619937">(Jun 22 2020 at 15:25)</a>:</h4>
<p>oh. you likely want <code>T::evacuate as fn() as *const u8</code>, because that gives you a pointer to the function, instead of a pointer to the ZST of the function, which idk what it actually is</p>



<a name="201620263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201620263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201620263">(Jun 22 2020 at 15:27)</a>:</h4>
<p>oh yea, <code>&amp;some_fn</code> gives you a pointer to the ZST, so that's some global anonymous static XD: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=2cf8304753334f7678b46dd5c3719b21">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=2cf8304753334f7678b46dd5c3719b21</a></p>



<a name="201635409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201635409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Avi Dessauer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201635409">(Jun 22 2020 at 17:23)</a>:</h4>
<p>Thanks, I don't know why, but I could have sworn <code>fn(&amp;u8, u8, u8, Range&lt;usize&gt;, &amp;mut Handlers)</code> gave an error.</p>



<a name="201696234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201696234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201696234">(Jun 23 2020 at 07:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209924">Avi Dessauer</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Making.20*const.20fn.20in.20const.20fn/near/201611814">said</a>:</p>
<blockquote>
<p>The function pointers are only called at run time. I use transmute as a convenience to turn <code>&amp;T</code> arguments into <code>*const u8</code></p>
</blockquote>
<p>you have a <em>pointer to</em> a function pointer though. (EDIT: or to something even stranger, like the ZST type associated with each function... "function handle"? I dint think we have a good name for it)</p>



<a name="201697784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201697784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201697784">(Jun 23 2020 at 08:08)</a>:</h4>
<p>you may actually get a pointer to the local on the stack I think</p>



<a name="201778758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201778758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201778758">(Jun 23 2020 at 20:32)</a>:</h4>
<blockquote>
<p>we have a good name for it</p>
</blockquote>
<p>function pointer pointer (repeat as needed)</p>



<a name="201778839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201778839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201778839">(Jun 23 2020 at 20:33)</a>:</h4>
<p>function pointer^2</p>



<a name="201828067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201828067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201828067">(Jun 24 2020 at 09:37)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> I dont meen ptrs to fn ptrs though</p>



<a name="201828082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201828082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201828082">(Jun 24 2020 at 09:37)</a>:</h4>
<p>I mean the type of <code>let f = function;</code> in Rust</p>



<a name="201828132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201828132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201828132">(Jun 24 2020 at 09:38)</a>:</h4>
<p>which is a ZST that uniquenely identifies the function</p>



<a name="201828140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Making%20%2Aconst%20fn%20in%20const%20fn/near/201828140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Making.20*const.20fn.20in.20const.20fn.html#201828140">(Jun 24 2020 at 09:38)</a>:</h4>
<p>in type theory we'd call it a singleton type</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>